#ifndef NATIVEAUDIOPLAYER_H
#define NATIVEAUDIOPLAYER_H

#include <QObject>

#include <sys/time.h>


#include <assert.h>
#include <jni.h>
#include <string.h>

// for __android_log_print(ANDROID_LOG_INFO, "YourApp", "formatted message");
#include <android/log.h>

// for native audio
#include <SLES/OpenSLES.h>
#include "SLES/OpenSLES_Android.h"

// for native asset manager
#include <sys/types.h>
#include <math.h>
#include <sbs2callback.h>
#include <QDateTime>


//#define FRAMES_SILENCE 88200
//#define FRAMES_600 4410
//#define FRAMES_1200 4410
//For Galaxy Note
#define FRAMES_600 4411
#define FRAMES_1200 4411
#define FRAMES_SILENCE 88222

//#define FRAMES_600 800
//#define FRAMES_1200 800
//#define FRAMES_SILENCE 16000


#define SAWTOOTH_FRAMES 8000
#define PI 3.14159265

class NativeAudioPlayer : public QObject
{
    Q_OBJECT
public:
    explicit NativeAudioPlayer(QObject *parent = 0);


private:
    short sawtoothBuffer[SAWTOOTH_FRAMES];
    short bufferSilence[FRAMES_SILENCE];
    short buffer600[FRAMES_600];
    short buffer1200[FRAMES_1200];
    SLObjectItf engineObject;
    SLEngineItf engineEngine;
    SLObjectItf outputMixObject;

    SLObjectItf bqPlayerObject;
    SLPlayItf bqPlayerPlay;
    SLAndroidSimpleBufferQueueItf bqPlayerBufferQueue;
    SLEffectSendItf bqPlayerEffectSend;

    static NativeAudioPlayer* m_pInstanse;

    int playbackIndex;
    int playingSingle;

    short *nextBuffer;
    unsigned nextSize;
    qint64 tt;

private:
    void createEngine();
    void createBufferQueueAudioPlayer();
    static void bqPlayerCallbackStatic(SLAndroidSimpleBufferQueueItf bq, void *context);
    void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context);

signals:
    void event(QString event);

public slots:
    void play();
    void playSingle();
    int playbackPosition();

};

#endif // NATIVEAUDIOPLAYER_H
